<?php
namespace app\wx\controller;
class User extends Basewx
{
    public function index(){
        $ua = $this->db->table('user_account')->where(['ua_id'=>session('wxgzh')['ua_id']])->item();
        $this->assign('name', $ua['ua_name']);
        $this->assign('data', session('wxgzh'));
        return $this->fetch('center');
    }
    public function center(){
        $ua = $this->db->table('user_account')->where(['ua_id'=>session('wxgzh')['ua_id']])->item();
        $this->assign('name', $ua['ua_name']);
        $this->assign('data', session('wxgzh'));
        return $this->fetch();
    }

    public function wallet(){
        $yhk = $this->db->table('channel_member')->where(['cm_phone'=>session('wxgzh')['phone']])->item();
        !$yhk&&exit("<script>alert('请联系业务员注册成为渠道商');window.location.href='/wx/user/center'</script>");
        $this->assign('msg', $yhk);
        return $this->fetch();
    }

    public function editinfo(){
        if($_POST){
//            $this->Tool->Tips('1','123',$_POST);
            $data = array();
            !empty($_POST['name'])&&$data['ua_name']=$_POST['name'];
            empty($_POST['phone'])&&$data['ua_phone']=session('wxgzh')['phone'];
            if(!empty($_POST['phone'])){
                empty($_POST['code'])&&$this->Tool->Tips('1','请输入验证码');
                $send = array('phone'=>$_POST['phone'],'code'=>$_POST['code']);
                $url = 'https://taoyuanapartment.com/v1/postcheckcode';
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_POST, true);//设置post方式提交
                curl_setopt($ch, CURLOPT_URL, $url);//设置抓取的url
                curl_setopt($ch, CURLOPT_POSTFIELDS, $send);//设置post数据
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以字符流的形式返回，而不是直接输出
                curl_setopt($ch,CURLOPT_HEADER,0);//是否输出页面内容
                curl_setopt($ch,  CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
                curl_setopt($ch, CURLOPT_TIMEOUT, 30);//抓取页面的等待时间为30秒
//curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
                $response = curl_exec($ch);//执行命令
                if ($response === FALSE){
                    echo 'cURL Error:'.curl_error($ch);
                }
                curl_close($ch);//关闭URL请求
                //处理返回数据
                $arr = json_decode($response,true);
                $response = json_encode($arr);
                if($arr['code']==0){
                    $data['ua_phone'] = $send['phone'];
                }else{
                    $this->Tool->Tips('1','验证码错误');
                }
            }
            !empty($_POST['pwd'])&&$data['ua_pwd']=md5(session('wxgzh')['ua_create_time'].$_POST['pwd']);
            $ua = $this->db->table('user_account')->where(['ua_id'=>session('wxgzh')['ua_id']])->update($data);
            $this->db->table('wxgzh')->where(['ua_id'=>session('wxgzh')['ua_id']])->update(['phone'=>$data['ua_phone']]);
            if ($ua) {
                $this->Tool->Tips('0','修改成功');
            }else{
                $this->Tool->Tips('1','修改失败');
            }

        }
        $ua = $this->db->table('user_account')->where(['ua_id'=>session('wxgzh')['ua_id']])->item();
        $this->assign('ua', $ua);
        return $this->fetch();
    }

}